<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="login-title">知识图谱管理系统</div>
        <div class="login-subtitle">请登录您的账号</div>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
        
        <div class="form-footer">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import axios from 'axios'

const { proxy } = getCurrentInstance()
const router = useRouter()
const loading = ref(false)
const rememberMe = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 构建表单数据
    const formData = new URLSearchParams()
    formData.append('username', loginForm.username)
    formData.append('password', loginForm.password)
    
    // 发送登录请求
    const response = await axios.post('http://localhost:8000/token', formData, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
    
    if (response.data.access_token) {
      // 保存token
      localStorage.setItem('token', response.data.access_token)
      
      // 获取用户信息
      const userResponse = await axios.get('http://localhost:8000/users/me', {
        headers: {
          Authorization: `Bearer ${response.data.access_token}`
        }
      })
      
      // 保存用户角色信息
      localStorage.setItem('isAdmin', userResponse.data.is_admin)
      localStorage.setItem('username', loginForm.username)
      
      proxy.$message.success('登录成功')
      router.push('/')
    }
  } catch (error) {
    console.error('Login error:', error)
    if (error.response?.data?.detail) {
      proxy.$message.error(error.response.data.detail)
    } else {
      proxy.$message.error('登录失败，请检查用户名和密码')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style>
html, body {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

.login-container {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #409EFF 0%, #36D1DC 100%);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--el-bg-color);
  border-radius: var(--el-border-radius-base);
  box-shadow: var(--el-box-shadow);
  padding: 40px;
  margin: 0;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin-bottom: 12px;
}

.login-subtitle {
  font-size: 16px;
  color: var(--el-text-color-secondary);
  line-height: 1.6;
}

.login-form {
  .el-form-item {
    margin-bottom: 24px;
  }
  
  .el-input {
    --el-input-height: 42px;
    
    .el-input__wrapper {
      background-color: var(--el-fill-color-blank);
      box-shadow: 0 0 0 1px var(--el-border-color) inset;
      
      &:hover {
        box-shadow: 0 0 0 1px var(--el-border-color-hover) inset;
      }
      
      &.is-focus {
        box-shadow: 0 0 0 1px var(--el-color-primary) inset;
      }
    }
    
    .el-input__prefix-inner {
      color: var(--el-text-color-secondary);
    }
  }
  
  .el-button {
    width: 100%;
    height: 42px;
    font-size: 16px;
    font-weight: 500;
    margin-top: 8px;
  }
}

.form-footer {
  margin-top: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: var(--el-text-color-regular);
  
  .el-checkbox {
    color: var(--el-text-color-regular);
  }
  
  .el-link {
    font-size: 14px;
    margin-left: 4px;
    
    &:hover {
      color: var(--el-color-primary-light-3);
    }
  }
}

@media screen and (max-width: 576px) {
  .login-card {
    padding: 30px 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .login-subtitle {
    font-size: 14px;
  }
}
</style> 